//
//  ViewController.m
//  代办事宜
//
//  Created by wang on 12-8-21.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "MainViewController.h"
#import "BackLogDetailViewController.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "JSONKit.h"

@interface MainViewController ()<UITableViewDelegate,UITableViewDataSource,ASIHTTPRequestDelegate>

@property(strong,nonatomic) UITableView* myTableView;

@end

@implementation MainViewController

@synthesize myTableView=_myTableView;


NSArray* eventTotalArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController.navigationBar setHidden:NO];
    [self.view setBackgroundColor:[UIColor clearColor]];
    self.title=@"代办事宜";
    UIBarButtonItem *topRightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reLoadMytableView:)];
    self.navigationItem.rightBarButtonItem = topRightButton;
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 
                                                                     self.view.bounds.size.height)
                                                    style:UITableViewStylePlain];      
    [self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.myTableView setDelegate:self];
    [self.myTableView setDataSource:self];
    [self.myTableView setBackgroundColor:[UIColor cyanColor]];
    [self.view addSubview: self.myTableView];
    
}

-(void) viewWillAppear:(BOOL)animated {
    [self sendRequest];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return  [NSString stringWithFormat:@"您有%d条代办事宜",[eventTotalArray count]];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [eventTotalArray count];
}

//row高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString* CellIdentifier = [NSString stringWithFormat:@"%d%d",indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        
    }
    for (UIView *v in [cell subviews]) {
        if ([v isKindOfClass:[UILabel class]] |
            [v isKindOfClass:[UITextView class]]) {
            [v removeFromSuperview];
        }
    }   
    NSUInteger index = [indexPath row];
    NSDictionary*total=[eventTotalArray objectAtIndex:index];
    
    //事务号
    UILabel*tempLabel4id = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 400, 30)];
    [tempLabel4id setText:[[NSString alloc] initWithFormat:@"事务ID号：%@", [total objectForKey:@"eventId"]]];
    [tempLabel4id setTextColor:[UIColor colorWithRed:9/255.0 green:119/255.0 blue:179/255.0 alpha:1.0]];
    tempLabel4id.font = [UIFont systemFontOfSize:20];
    [tempLabel4id setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:tempLabel4id];
    
    //事务模板
    UILabel*tempLabel4model = [[UILabel alloc] initWithFrame:CGRectMake(450, 10, 500, 30)];
    [tempLabel4model setText:[[NSString alloc] initWithFormat:@"事务模板：%@",[total objectForKey:@"modelName"]]];
    [tempLabel4model setTextColor:[UIColor colorWithRed:9/255.0 green:119/255.0 blue:179/255.0 alpha:1.0]];
    tempLabel4model.font = [UIFont systemFontOfSize:20];
    [tempLabel4model setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:tempLabel4model];
    
    //事务发起人
    UILabel*tempLabel4creator = [[UILabel alloc] initWithFrame:CGRectMake(10, 50, 400, 30)];
    [tempLabel4creator setText:[[NSString alloc] initWithFormat:@"提交人   ：%@",[total objectForKey:@"userName"]]];
    [tempLabel4creator setTextColor:[UIColor colorWithRed:9/255.0 green:119/255.0 blue:179/255.0 alpha:1.0]];
    tempLabel4creator.font = [UIFont systemFontOfSize:20];
    [tempLabel4creator setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:tempLabel4creator];
    
    //事务发起时间
    UILabel*tempLabel4createDate = [[UILabel alloc] initWithFrame:CGRectMake(450, 50, 400, 30)];
    //NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    //NSString* dateStr = [total objectForKey:@"createDate"];
    //[formatter setDateStyle:NSDateFormatterFullStyle];
    //NSDate* date=[formatter dateFromString:dateStr];
    [tempLabel4createDate setText:[[NSString alloc] initWithFormat:@"提交时间:2012-08-01 12:30"]];
    [tempLabel4createDate setTextColor:[UIColor colorWithRed:9/255.0 green:119/255.0 blue:179/255.0 alpha:1.0]];
    tempLabel4createDate.font = [UIFont systemFontOfSize:20];
    [tempLabel4createDate setBackgroundColor:[UIColor clearColor]];
    [cell addSubview:tempLabel4createDate];
    
    
    //事务标题
    UILabel*tempLabel4title = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 800, 30)];
    [tempLabel4title setText:[[NSString alloc] initWithFormat:@"事务标题：%@",[total objectForKey:@"eventTitle"]]];
    [tempLabel4title setTextColor:[UIColor colorWithRed:9/255.0 green:119/255.0 blue:179/255.0 alpha:1.0]];
    tempLabel4title.font = [UIFont systemFontOfSize:20];
    [tempLabel4title setBackgroundColor:[UIColor clearColor]];
    tempLabel4title.lineBreakMode = UILineBreakModeWordWrap; 
    tempLabel4title.numberOfLines = 0;
    [tempLabel4title sizeToFit];
    [cell addSubview:tempLabel4title];
    
    UIImage* seletedImg = [UIImage imageNamed:@"tableviewcellSelected.png"];
    UIImageView *seletedImgView =[[UIImageView alloc] initWithImage:seletedImg];
    [cell setSelectedBackgroundView:seletedImgView];
    
    UIImage* backImg = [UIImage imageNamed:@"tableviewcell.png"];
    UIImageView *backImgView =[[UIImageView alloc] initWithImage:backImg];
    [cell setBackgroundView:backImgView];
    
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {   
    
    BackLogDetailViewController* myDetailView = [[BackLogDetailViewController alloc] init];
    
    [myDetailView passFromtotal:[eventTotalArray objectAtIndex:indexPath.row]];
    
    [self.navigationController pushViewController:myDetailView animated:YES];
    
}


- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)requestFinished:(ASIHTTPRequest *)request {
    
    NSError *error = [request error];
    if (!error) {
        NSString *res = [request responseString];
        eventTotalArray= [res objectFromJSONString];
        [self hideMbProgress];
    }    
    [self.myTableView reloadData];
}

-(void)reLoadMytableView:(id) sender{
    [self sendRequest];
}

-(void)sendRequest{
    
    NSString* u=[[NSUserDefaults standardUserDefaults]objectForKey:@"WebserviceUrl"];
    NSURL *url = [NSURL URLWithString:u];    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];    
    [request addPostValue: @"getProcessEventList" forKey:@"method"];
    [self showMbprogress];
    [request setDelegate:self];
    [request start];
}
@end
